package com.dingshi.security;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.UserApi;
import com.dingshi.security.db.SettingLoader;
import com.dingshi.security.util.Utils;
public class SettingActivity extends BaseActivity implements OnClickListener{
	private Button btn_loginout;
	private Button btn_exit;
	private Button btn_about_soft;
	private Button btn_save;
	private EditText edit_svr_path;
	private EditText edit_update_svr_path;
	private Button btn_back;
	private TextView title_text;
	private Button btn_reset;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		
		initTopBar();
		initComponents();
		registerButton();
	}
	
	private void initTopBar() {
		btn_back = (Button)findViewById(R.id.btn_back);
		title_text = (TextView)findViewById(R.id.title_text);
		btn_reset = (Button)findViewById(R.id.btn_refresh);
		btn_reset.setVisibility(View.GONE);
		title_text.setText("设置");
		btn_back.setOnClickListener(this);
		
	}

	private void registerButton() {
		btn_loginout.setOnClickListener(this);
		btn_exit.setOnClickListener(this);
		btn_about_soft.setOnClickListener(this);
		btn_save.setOnClickListener(this);
	}

	private void initComponents() {
		btn_loginout = (Button)findViewById(R.id.btn_loginout);
		btn_exit = (Button)findViewById(R.id.btn_exit);
		btn_about_soft = (Button)findViewById(R.id.btn_about_soft);
		btn_save = (Button)findViewById(R.id.btn_save);
		
		
		edit_svr_path = (EditText)findViewById(R.id.edit_svr_path);
		edit_update_svr_path = (EditText)findViewById(R.id.edit_update_svr_path);
		edit_svr_path.setText(SettingLoader.getSvrPath(this));
		edit_update_svr_path.setText(SettingLoader.getUpdateSvrPath(this));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_loginout:
			new AlertDialog.Builder(this)
			.setTitle(getResources().getString(R.string.menu_exit))
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setMessage(getResources().getString(R.string.menu_exit_message))
			.setCancelable(false)
			.setPositiveButton(getResources().getString(R.string.confirm),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							SettingLoader.setUserInfo(SettingActivity.this, "");
							Intent intent = new Intent(SettingActivity.this,LoginActivity.class);
							startActivity(intent);
						}
					})
			.setNegativeButton(getResources().getString(R.string.cancel),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							dialog.dismiss();
						}
					}).show();
			break;
		case R.id.btn_exit:
			LinearLayout line = new LinearLayout(this);
			line.setOrientation(LinearLayout.VERTICAL);
			LinearLayout.LayoutParams textLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
			textLP.setMargins(Utils.dipToPixels(this, 10), 0, Utils.dipToPixels(this, 10), 0);
			TextView text = new TextView(this);
			text.setText("请输入密码");
			line.addView(text,textLP);
			LinearLayout.LayoutParams editLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
			editLP.setMargins(Utils.dipToPixels(this, 10), 0, Utils.dipToPixels(this, 10), 0);
			final EditText edit = new EditText(this);
			line.addView(edit,editLP);			
			new AlertDialog.Builder(this)
			.setTitle(getResources().getString(R.string.menu_exit))
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setView(line)
			.setCancelable(false)
			.setPositiveButton(getResources().getString(R.string.confirm),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							authPwd(edit);
						}
					})
			.setNegativeButton(getResources().getString(R.string.cancel),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							dialog.dismiss();
						}
					}).show();
			break;
		case R.id.btn_about_soft:
			Intent intent = new Intent(this,SoftAboutActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_save:
			
			String svrpath = edit_svr_path.getText().toString();
			String updatepath = edit_update_svr_path.getText().toString();
			if(Utils.isNullOrEmpty(svrpath)) {
				Utils.showToast(this, "登录服务器地址不能为空！");
				return;
			}
			if(Utils.isNullOrEmpty(updatepath)) {
				
			}
			if(SettingLoader.setSvrPath(this,svrpath,updatepath)) {
				Utils.showToast(this, "保存成功！");
			}else {
				Utils.showToast(this, "保存失败！");
			}
			break;
		case R.id.btn_back:
			finish();
			break;
		default:
			break;
		}
	}
	
	private void authPwd(EditText edit) {
		String pwd = edit.getText().toString();
		if(Utils.isNullOrEmpty(pwd)) {
			Utils.showToast(this, "请输入密码");
			return ;
		}
		UserApi api = new UserApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {
			
			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				JSONObject jsonEntity = apiResult.getJsonEntity();
				if (jsonEntity != null) {
					// {"d":"双兴监控室","t":"1","r":"0","nid":"1","a":"双兴小区","n":"张三","did":"4","aid":"2"}
					// {"d":{"a":"双兴小区","aid":"2","d":"双兴监控室","did":"4","n":"张三","nid":"1","r":"0","t":"1",},"t":0}
					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							JSONObject json = jsonEntity.getJSONObject("d");
							String result = json.getString("r");
							if ("1".equals(result)) {
								SecurityApp.getApp(SettingActivity.this).exitApp();
							} 
							break;
						case 1:// 数组

							break;
						case -1:// 错误

							break;

						default:
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
			}
			
			@Override
			public <T> void onReturnLoading(int requestCode) {
				
			}
			
			@Override
			public <T> void onReturnFailResult(int requestCode, ApiResult<T> apiResult) {
				
			}
		});
		api.LoginOut(1, this, pwd);
	}
}
